<template>
  <div class="login">
    <!-- 头部 -->
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)"></van-nav-bar>
    <!-- 主体 -->
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input v-model="phoneNum" class="inp" maxlength="11" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input class="inp" v-model="verifyValue" maxlength="5" placeholder="请输入图形验证码" type="text">
          <img :src="verifyUrl" v-if="verifyUrl" @click="getImgVerify" alt="">
        </div>
        <div class="form-item">
          <input v-model="msgCode" class="inp" placeholder="请输入短信验证码" type="text">
          <button @click="getMes">{{ mesCount === 15 ? '获取验证码' : `请${mesCount}后重新获取`}}</button>
        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>

<script>

import { getImgAsync, getMesAsync, postLoginAsync } from '@/api/login.js'

export default {

  name: 'DetailIndex',
  data () {
    return {
      verifyUrl: '', // 验证码图片地址
      verifyId: '', // 验证码id
      verifyValue: '', // 输入的验证码值
      phoneNum: '', // 手机号
      mesCount: 15, // 短信验证码倒计时
      mesTimer: null, // 短信验证码倒计时定时器
      msgCode: '' // 短信验证码
    }
  },
  created () {
    this.getImgVerify()
  },
  methods: {
    // 获取图形验证码
    async getImgVerify () {
      const res = await getImgAsync()
      this.verifyUrl = res.data.base64
      this.verifyId = res.data.key
    },
    // 获取短信验证码
    async getMes () {
      if (!this.verify()) return // 校验手机验证码

      if (this.mesCount === 15 && !this.mesTimer) {
        // 开启计时
        this.mesTimer = setInterval(() => {
          this.mesCount -= 1
          if (this.mesCount <= 0) {
            clearInterval(this.mesTimer)
            this.mesCount = 15
            this.mesTimer = null
          }
        }, 1000)
        // 请求
        const res = await getMesAsync({
          captchaCode: this.verifyValue,
          captchaKey: this.verifyId,
          mobile: this.phoneNum
        })
        console.log(res)
        this.$toast('短信验证码已发送')
      }
    },
    // 校验函数
    verify () {
      if (!/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(this.phoneNum)) {
        this.$toast('请输入正确的手机号')
        return false
      }
      if (!/^\w{4}$/i.test(this.verifyValue)) {
        this.$toast('请输入正确的验证码')
        return false
      }
      return true
    },
    // 登录请求
    async login () {
      if (!this.verify()) {
        return
      }
      const url = this.$route.query.redirect || '/'
      const res = await postLoginAsync(this.phoneNum, this.msgCode)
      this.$store.commit('user/setUserInfo', res.data)
      this.$router.replace(url)
      this.$toast('登录成功')
    }
  },
  destroyed () { // 销毁时清除定时器
    if (this.mesTimer) {
      clearInterval(this.mesTimer)
      this.mesTimer = null
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
